﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using WqLunTan.Areas.Manage.Models;
using WqLunTan.Filters;
using WqLunTan.Models.DataBase;

namespace WqLunTan.Areas.Manage.Controllers
{
    /// <summary>
    /// 后台占卜管理 控制器
    /// </summary>
    [Auth(AuthType = WqUserAuth.Manage)]
    public class DivinationController : BaseManageController
    {
        /// <summary>
        /// 已经占卜完成的列表
        /// </summary>
        /// <returns></returns>
        public IActionResult DiviningList(string search, int pageIndex = 1, int pageSize = 20)
        {
            List<article_forecast> list = null;

            var query = DbContext.art_forecast.Where(x => x.is_show);
            var page = new PageModel() { CurrentPageIndex = pageIndex };

            if (!string.IsNullOrWhiteSpace(search))
            {
                query = query.Where(x => x.horoscope_8char.Equals(search) || x.name.Equals(search));
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.create_time).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }
            else
            {
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.create_time).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }

            ViewData["page"] = page;
            ViewData["list"] = list;
            ViewData["is_show"] = true;
            ViewData["show-count"] = DbContext.art_forecast.Where(x => x.is_show).Count();
            ViewData["hide-count"] = DbContext.art_forecast.Where(x => x.is_show == false).Count();

            return View();
        }


        public IActionResult DiviningHideList(string search, int pageIndex = 1, int pageSize = 20)
        {
            List<article_forecast> list = null;
            var query = DbContext.art_forecast.Where(x => x.is_show == false);
            var page = new PageModel() { CurrentPageIndex = pageIndex };

            if (!string.IsNullOrWhiteSpace(search))
            {
                query = query.Where(x => x.horoscope_8char.Equals(search) || x.name.Equals(search));
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.create_time).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }
            else
            {
                page.TotalCount = query.Count();
                list = query.OrderByDescending(x => x.create_time).Skip(pageSize * pageIndex - pageSize).Take(pageSize).ToList();
            }

            ViewData["page"] = page;
            ViewData["list"] = list;
            ViewData["is_show"] = false;
            ViewData["show-count"] = DbContext.art_forecast.Where(x => x.is_show).Count();
            ViewData["hide-count"] = DbContext.art_forecast.Where(x => x.is_show == false).Count();

            return View("DiviningList");
        }

        /// <summary>
        /// 详情页
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public IActionResult DivinationDetail(Guid id)
        {
            if (id != Guid.Empty)
            {
                var model = DbContext.art_forecast.Where(x => x.id == id).SingleOrDefault() ?? new article_forecast();
                return View(model);
            }
            return JsonFailureResult("请求失败,未获取到数据");
        }

        /// <summary>
        /// 简批八字
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult UpdateDivination(article_forecast model)
        {
            if (!string.IsNullOrWhiteSpace(model.horoscope_8char) && !string.IsNullOrWhiteSpace(model.horoscope_result) && !string.IsNullOrWhiteSpace(model.title))
            {
                var art = DbContext.art_forecast.Where(x => x.id == model.id).SingleOrDefault();
                if (art != null)
                {
                    art.horoscope_8char = model.horoscope_8char;
                    art.horoscope_result = model.horoscope_result;
                    art.is_show = model.is_show;
                    art.change_user = LoginUser.UserName;
                    art.last_update = DateTimeOffset.UtcNow;
                    art.title = model.title;

                    DbContext.SaveChanges();
                }
            }
            return JsonOkResult("修改成功");
        }

    }
}